package demo1;

import java.util.Arrays;
import java.util.Stack;

public class MyStack {

      /* public int[] array;
       public static final int k = 3;
       public MyStack(){
          this.array = new int[3];
       }

    public int usedSize = 0;

   //压栈
    public int push(int data){
        //判满
        isFull();
         array[usedSize++] = data;
         return data;
    }
    //出栈
    public int pop(){
        return array[--usedSize];
    }


    public boolean isEmpty(){
        return usedSize == 0;
    }
    public int peek(){
        return array[--usedSize];
    }
    //判断栈是否满了
    public void isFull(){
        if(usedSize == array.length){
            array = Arrays.copyOf(array,usedSize*2);
        }
    }*/


    Stack<Integer> stack;
    Stack<Integer> minStack;

    public MyStack() {
        stack = new Stack<>();
        minStack = new Stack<>();
    }

    public void push(int val) {
        stack.push(val);
        //第一次在最小栈中存储元素
        if (minStack.empty()) {
            minStack.push(val);
        } else {
            if (val <= minStack.peek()) {
                minStack.push(val);
            }
        }

    }

    public void pop() {
        //栈为空则不能弹出元素
        if (stack.empty()) {
            return;
        }
        int val = stack.pop();
        if (val == minStack.peek()) {
            minStack.pop();
        }

    }

    public int top() {
        if (stack.empty()) {
            return -1;
        }
        return stack.peek();
    }

    public int getMin() {
        return minStack.peek();
    }

}
